<html>
<div id="myheader">
<?php
session_start();
        include "include/top.php";
?>
</div>

<title>OSPMS</title>
<head>
<script type="text/javascript">
<!--
 function ExChgClsName(Obj,NameA,NameB){
  var Obj=document.getElementById(Obj)?document.getElementById(Obj):Obj;
  Obj.className=Obj.className==NameA?NameB:NameA;
 }
 function showMenu(iNo){
 ExChgClsName("Menu_"+iNo,"MenuBox","MenuBox2");
 }
-->
</script>
</head>

<link rel="stylesheet" href="style/ospms-body.css" type="text/css">
<?php
  include "include/left-menu.php";
?>

<body>
<div id="mymajorbody">
<div id="mybody">

<?php
echo "<table style='border: solid 1px black;'>";

$dbservername = "localhost";
$dbusername = "ospms";
$dbpassword = "ospms";
$dbname = "OSPMS";

session_start();

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "\n";
    }
}


try {
    $conn = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$query = 'SELECT * FROM user_projects';
$select = $conn->query($query);

$total_column = $select->columnCount();
//var_dump($total_column);

for ($counter = 0; $counter <= $total_column; $counter ++) {
    $meta = $select->getColumnMeta($counter);
    $column[] = $meta['name'];
}

$username = $_SESSION['user_name'];
$user_id = $_SESSION['user_id'];

$table_project_fields = array (
  0 => '<b>Project Name</b>',
  1 => '<b>Project Description</b>',
);

    $query = "SELECT project_name,project_description FROM user_projects where user_id = ". $user_id;
    $stmt = $conn->prepare($query);
    $stmt->execute();
    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$array_project = $stmt->fetchAll();
//var_dump($array_project);

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
//        echo $v;
    }
$table2_study_fields = array (
  0 => 'study_name',
  1 => 'study_description',
  2 => 'Upload File to Study',
);

    $query = "SELECT study_name,study_description FROM user_studies where user_id = " .$user_id. " and project_id = 1";
    $stmt = $conn->prepare($query);
    $stmt->execute();
    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$array_study = $stmt->fetchAll();

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
//        echo $v;
    }

    $dsn = null;

  echo '<div id="project_list">';
  echo "<tr>". " \n";
for ( $i=0 ; $i<count($table_project_fields); $i++ ) {
  echo "<td>" ;
  print_r($table_project_fields[$i]);
  echo "</td>" ;
}
  echo "</tr>". " \n";
  
//var_dump($array_project);
foreach ($array_project as $key => $value) {
  echo "<tr>". " \n";
  $sub_prj = $value;
  foreach ($sub_prj as $sub_key => $sub_value )
  {
      echo "<td>";
#      var_dump($sub_value);
      print_r($sub_value);
      echo "</td>";
  }
  echo "</tr>". " \n";
  if ( $key == 'test1' )
  {
//# check method 1
//    echo "\n</tr>\n";
//    echo "<tr>  <td>1</td><td>2</td>\n";

//# check method 2
//	var_dump($array_study);

// method 3
        echo '<form method="get" enctype="multipart/form-data" action="/OSPMS/Upload_Project.php">';
	echo "<div>";
        echo "<tr><td><b>Study</b></td><td><b>Study Description</b></td>></tr>\n";
        echo "<tr>\n";
        foreach($array_study as $substudy => $substudy_item)
        {
//              print_r($substudy_item);
              echo "<tr>\n";
//	      $my_study_name = "Study";
//	      var_dump($substudy_item);
	      $my_study_name = $substudy_item['study_name'];
              foreach($substudy_item as $subsubitm => $subsubvalue )
              {
                  echo "<td>\n";
                  print_r($subsubvalue);
                  echo "</td>\n";
              }
              echo "<td class=\"righttd\"> <input type=\"submit\" value=\"Upload File to " .$my_study_name. "\" name=\"add\" id=\"cmdLogin\"/> </td>\n";
              echo "</tr>\n";
        }
        echo "</tr>\n";
	echo "</div>";
	echo "</form>";

  }
}


  echo '<div id="study_list">';
  echo "<tr>". " \n";
for ( $j=0 ; $j<count($table2_study_fields); $j++ ) {
  echo "<td>" ;
  print_r($table2_fields[$j]);
  echo "</td>" ;
}
  echo "</tr>". " \n";
  echo "</div>";
  echo "</div>";

//var_dump($array_project);
//var_dump($array_study);


} // try
catch(PDOException $e)
{
    echo "Error: " . $e->getMessage();
} // try - catch



$conn = null;
echo "</table>";
?> 



</div>
</div>



<div>

  <link rel="stylesheet" href="style/ospms.css" type="text/css">
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  <script language="JavaScript" type="text/javascript" src="Project_Library_files/jquery.js"></script>
  <script language="JavaScript" type="text/javascript" src="Project_Library_files/ospms.js"></script>


<!--
<table id="headerHolder" cellpadding="0" cellspacing="0"><tbody><tr><td>
<div id="header">
 <ul>
  <li><a href="./index.html" title="programs">Programs</a></li>
  <li><a href="./index.html" title="projects">Projects</a></li>
  <li><a href="./index.html" title="Addprojects">AddProjects</a></li>
  <li><a href="utilities.html" title="studies">Studies</a></li>
  <li><a href="./index.html" title="uploaded">Uploaded_Files</a></li>
 </ul>
</div>
</td></tr></tbody></table>
-->




<div class="content" style="clear:both;">

&nbsp;
</td></tr>


<tr valign="top"><td class="container" align="left" valign="top">
<p>

<!--
</p><form name="options" action="">
 <label for="showComments"><input id="showComments" type="checkbox">Upload File</label>
-->
<!-- <label for="showLegend"><input checked="checked" id="showLegend" type="checkbox">Legend</label> -->
<!--
</form>
-->


</td></tr></tbody></table>
<div class="indent">

<!-- <b>contains __5__ studies</b>  sort:<a name="columns"></a> -->

<!--


<table id="columns" class="dataTable" rules="groups" border="1">
<colgroup>
</colgroup><colgroup>
</colgroup><colgroup>
</colgroup><colgroup>
</colgroup><colgroup>
</colgroup><colgroup>
</colgroup><colgroup>
</colgroup><colgroup class="comment">
</colgroup><thead align="left">
<tr>
  <th class="sortedByColumn">Table</th>
  <th class="notSortedByColumn"><a href="#"><span class="notSortedByColumn">Study_Name</span></a></th>
  <th class="notSortedByColumn"><a href="#"><span class="notSortedByColumn">Study_Type</span></a></th>
  <th class="notSortedByColumn"><a href="#"><span class="notSortedByColumn">Number_of_Files</span></a></th>
  <th title="Are nulls allowed?" class="notSortedByColumn"><a href="#"><span class="notSortedByColumn">Revision</span></a></th>
  <th title="Is column automatically updated?" class="notSortedByColumn"><a href="#"><span class="notSortedByColumn">Date</span></a></th>
  <th title="Default value" class="notSortedByColumn"><a href="#"><span class="notSortedByColumn">Description</span></a></th>
  <th title="Comments" class="comment"><span class="notSortedByColumn">Upload File</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="even">
 <td class="detail"><a href="#">Study #1</a></td>
 <td class="detail">Study #1</td>
 <td class="detail">study_type</td>
 <td class="detail" align="right">50</td>
 <td class="detail" align="center"></td>
 <td class="detail" align="center"></td>
 <td class="detail"></td>
 <td class="comment detail">Upload_Button</td>
</tr>
<tr class="even">
 <td class="detail"><a href="#">Study #2</a></td>
 <td class="detail">state</td>
 <td class="detail">another_type</td>
 <td class="detail" align="right">2</td>
 <td class="detail" align="center"></td>
 <td class="detail" align="center"></td>
 <td class="detail"></td>
 <td class="comment detail">Upload_Button</td>
</tr>

</tbody></table>

-->


</div>
</div>


</div>  <!-- new section -->








<div id="myfooter">
<?php
        include "include/bottom.php"
?>
</div>
